sqlalchemy.orm relationship Example Code

您所在的位置:网站首页 sqlalchemy sql sqlalchemy.orm relationship Example Code

sqlalchemy.orm relationship Example Code

2023-01-08 20:22| 来源: 网络整理| 查看: 265

relationship is a callable within the sqlalchemy.orm module of the SQLAlchemy project.

ColumnProperty, CompositeProperty, Load, Mapper, Query, RelationshipProperty, Session, SynonymProperty, aliased, attributes, backref, class_mapper, column_property, composite, interfaces, mapper, mapperlib, object_mapper, object_session, query, session, sessionmaker, and strategies are several other callables with code examples from the same sqlalchemy.orm package.

Example 1 from graphene-sqlalchemy

graphene-sqlalchemy (project documentation and PyPI package information) is a SQLAlchemy integration for Graphene, which makes it easier to build GraphQL-based APIs into Python web applications. The package allows you to subclass SQLAlchemy classes and build queries around them with custom code to match the backend queries with the GraphQL-based request queries. The project is provided as open source under the MIT license.

graphene-sqlalchemy / graphene_sqlalchemy / tests / models.py

# models.py from __future__ import absolute_import import enum from sqlalchemy import (Column, Date, Enum, ForeignKey, Integer, String, Table, func, select) from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import column_property, composite, mapper, relationship PetKind = Enum("cat", "dog", name="pet_kind") class HairKind(enum.Enum): LONG = 'long' SHORT = 'short' Base = declarative_base() association_table = Table( "association", Base.metadata, Column("pet_id", Integer, ForeignKey("pets.id")), Column("reporter_id", Integer, ForeignKey("reporters.id")), ) class Editor(Base): __tablename__ = "editors" editor_id = Column(Integer(), primary_key=True) name = Column(String(100)) ## ... source file abbreviated to get to relationship examples ... pet_kind = Column(PetKind, nullable=False) hair_kind = Column(Enum(HairKind, name="hair_kind"), nullable=False) reporter_id = Column(Integer(), ForeignKey("reporters.id")) class CompositeFullName(object): def __init__(self, first_name, last_name): self.first_name = first_name self.last_name = last_name def __composite_values__(self): return self.first_name, self.last_name def __repr__(self): return "{} {}".format(self.first_name, self.last_name) class Reporter(Base): __tablename__ = "reporters" id = Column(Integer(), primary_key=True) first_name = Column(String(30), doc="First name") last_name = Column(String(30), doc="Last name") email = Column(String(), doc="Email") favorite_pet_kind = Column(PetKind) pets = relationship("Pet", secondary=association_table, backref="reporters", order_by="Pet.id") articles = relationship("Article", backref="reporter") favorite_article = relationship("Article", uselist=False) @hybrid_property def hybrid_prop(self): return self.first_name column_prop = column_property( select([func.cast(func.count(id), Integer)]), doc="Column property" ) composite_prop = composite(CompositeFullName, first_name, last_name, doc="Composite") class Article(Base): __tablename__ = "articles" id = Column(Integer(), primary_key=True) headline = Column(String(100)) pub_date = Column(Date()) reporter_id = Column(Integer(), ForeignKey("reporters.id")) class ReflectedEditor(type): @classmethod def __subclasses__(cls): ## ... source file continues with no further relationship examples... Example 2 from sqlalchemy-datatables

sqlalchemy-datatables (PyPI package information) is a helper library that makes it easier to use SQLAlchemy with the jQuery JavaScript DataTables plugin. This library is designed to be web framework agnostic and provides code examples for both Flask and Pyramid.

The project is built and maintained by Michel Nemnom (Pegase745) and is open sourced under the MIT license.

sqlalchemy-datatables / tests / models.py

# models.py import datetime from sqlalchemy import Column, Date, DateTime, ForeignKey, Integer, String, func from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import backref, relationship Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String, unique=True) created_at = Column(DateTime, default=datetime.datetime.utcnow) birthday = Column(Date) address = relationship('Address', uselist=False, backref=backref('user')) def __unicode__(self): return '%s' % self.name def __repr__(self): return '' % (self.__class__.__name__, self.id) @hybrid_property def dummy(self): return self.name[0:3] @dummy.expression def dummy(cls): return func.substr(cls.name, 0, 3) class Address(Base): __tablename__ = 'addresses' id = Column(Integer, primary_key=True) description = Column(String, unique=True) user_id = Column(Integer, ForeignKey('users.id')) ## ... source file continues with no further relationship examples...


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3